﻿using System;
using Landpy.CruiseControlNet.ConfigurationSidekick.ConfigTreeModule;
using Landpy.CruiseControlNet.ConfigurationSidekick.ParserModule;

namespace Landpy.CruiseControlNet.ConfigurationSidekick.ConfigPropertyModule
{
    class PermissionHandler : GetPropertiesHandler
    {
        public override object GetPropertiesObject(GetPropertiesContext getPropertiesContext)
        {
            var treeNodeIndentity = this.GetTreeNodeIndentity(getPropertiesContext);
            if (treeNodeIndentity.Type == TreeNodeType.InternalRolePermission)
            {
                return this.PermissionRepository.GetInternalRolePermissionById(
                        getPropertiesContext.CruiseControl.InternalSecurity, new Guid(treeNodeIndentity.Key));
            }
            if (treeNodeIndentity.Type == TreeNodeType.InternalUserPermission)
            {
                return this.PermissionRepository.GetInternalUserPermissionById(
                    getPropertiesContext.CruiseControl.InternalSecurity, new Guid(treeNodeIndentity.Key));
            }
            if (treeNodeIndentity.Type == TreeNodeType.ProjectRolePermission)
            {
                ProjectChildCollectionKey projectChildCollectionKey =
                    ProjectChildCollectionKeyUtil.GetKey(treeNodeIndentity.Key);
                return this.PermissionRepository.GetProjectRolePermissionById(getPropertiesContext.CruiseControl,
                                                                              projectChildCollectionKey.ProjectId,
                                                                              projectChildCollectionKey.Id);
            }
            if (treeNodeIndentity.Type == TreeNodeType.ProjectUserPermission)
            {
                ProjectChildCollectionKey projectChildCollectionKey =
                    ProjectChildCollectionKeyUtil.GetKey(treeNodeIndentity.Key);
                return this.PermissionRepository.GetProjectUserPermissionById(getPropertiesContext.CruiseControl,
                                                                              projectChildCollectionKey.ProjectId,
                                                                              projectChildCollectionKey.Id);
            }
            return this.Successor.GetPropertiesObject(getPropertiesContext);
        }
    }
}
